//
//  HomeHeader.m
//  预购清单
//
//  Created by  on 2022/10/16.
//

#import "HomeHeader.h"

@interface HomeHeader()<EPCycleDelegate>
@property (strong, nonatomic) IBOutlet UIButton *footbtn;
@property (strong, nonatomic) IBOutlet UIButton *basketBtn;
@property (strong, nonatomic) IBOutlet UIButton *allBtn;
@property (strong, nonatomic) IBOutlet UIButton *scheduleBtn;
@property (strong, nonatomic) IBOutlet UIButton *resultBtn;
@property (strong, nonatomic) IBOutlet UIView *line;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *lineCenter1;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *lineCenter2;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *lineCenter3;
@property (nonatomic,strong) EPCycle *cycle;
@property (nonatomic,strong) NSArray *btns;
@property (nonatomic,strong) NSArray *centers;
@property (nonatomic,strong) NSArray *imgs;

@end
@implementation HomeHeader
- (void)awakeFromNib{
    [super awakeFromNib];
    self.imgs = @[@"banner1",@"banner2"];
    self.btns = @[self.allBtn,self.scheduleBtn,self.resultBtn];
    self.centers = @[self.lineCenter1,self.lineCenter2,self.lineCenter3];
    [self addSubview:self.cycle];
    CCLayoutConstraint *left = [CCLayoutConstraint a:self.cycle attr:CCLeft r:CCEqual b:self attr:CCLeft m:1 c:0];
    CCLayoutConstraint *right = [CCLayoutConstraint a:self.cycle attr:CCRight r:CCEqual b:self attr:CCRight m:1 c:0];
    CCLayoutConstraint *bottom = [CCLayoutConstraint a:self.cycle attr:CCBottom r:CCEqual b:self attr:CCBottom m:1 c:0];
    CCLayoutConstraint *top = [CCLayoutConstraint a:self.cycle attr:CCTop r:CCEqual b:self attr:CCTop m:1 c:self.frame.size.height - 200];
    [self addConstraints:@[left,right,bottom,top]];
}
- (void)setSelectFootball:(BOOL)selectFootball{
    _selectFootball = selectFootball;
    self.footbtn.selected = selectFootball;
    self.basketBtn.selected = !selectFootball;
    self.footbtn.bgHexColor = self.footbtn.selected ? @"#FFFFFF":@"#2756CA";
    self.basketBtn.bgHexColor = self.basketBtn.selected ? @"#FFFFFF":@"#2756CA";
}
- (IBAction)actionForBtn:(UIButton *)sender {
    if (self.block) {
        if (sender.tag == 203) {
            return self.block(@"刷新");
        }
        self.block(sender.currentTitle);
    }
    if (sender.tag < 203) {
        self.footbtn.selected = [self.footbtn isEqual:sender];
        self.basketBtn.selected = [self.basketBtn isEqual:sender];
        self.footbtn.bgHexColor = self.footbtn.selected ? @"#FFFFFF":@"#2756CA";
        self.basketBtn.bgHexColor = self.basketBtn.selected ? @"#FFFFFF":@"#2756CA";
    }else if (sender.tag > 203){
        for (int i = 0; i < self.btns.count;i++) {
            UIButton *btn = self.btns[i];
            NSLayoutConstraint *center = self.centers[i];
            btn.selected = [btn isEqual:sender];
            btn.titleLabel.font = [UIFont systemFontOfSize:16 weight:btn.selected ? UIFontWeightBold : UIFontWeightRegular];
            center.active = btn.selected;
        }
    }
}
-(EPCycle *)cycle{
    if (!_cycle) {
        _cycle = (EPCycle*)[EPCycle loadFromXIB];
        _cycle.translatesAutoresizingMaskIntoConstraints = NO;
        _cycle.delegate = self;
        _cycle.num = self.imgs.count;
    }
    return _cycle;
}
#pragma mark - EPCycleDelegate
- (void)cellOfIndex:(UICollectionViewCell *)cell :(NSIndexPath *)indexpath{
    [cell.contentView removeAllSubView];
    NSString *name = indexpath.row <= 1 ? self.imgs[0] : self.imgs[1];
    UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:name]];
    img.contentMode = UIViewContentModeScaleAspectFill;
    [cell.contentView addSubview:img];
    img.c_edges.equalTo(cell.contentView);
}
- (void)selectedCell:(NSIndexPath *)indexpath{
    
}
@end
